package com.letfox.common.model;

import org.apache.commons.lang.StringUtils;

import com.letfox.common.enums.ResponseEnum;
import com.letfox.common.exception.CustomerException;

/**
 * 前台响应模型
 */
public class ResultResponse {

	private String statusCode;
	private String statusMessage;
	private Object data;

	/**
	 * 响应成功
	 * @param message
	 * @return
	 */
	public static ResultResponse getSuccessInstance(String message) {
		ResultResponse resultResponse = new ResultResponse();
		resultResponse.setStatusCode(ResponseEnum.R200.getCode());
		resultResponse.setStatusMessage(StringUtils.isNotBlank(message) ? message : ResponseEnum.R200.getMessage());
		return resultResponse;
	}
	
	/**
	 * 响应失败
	 * @param message
	 * @return
	 */
	public static ResultResponse getFailInstance(String message) {
        ResultResponse resultResponse = new ResultResponse();
        resultResponse.setStatusCode(ResponseEnum.R503.getCode());
        resultResponse.setStatusMessage(StringUtils.isNotBlank(message) ? message : ResponseEnum.R503.getMessage());
        return resultResponse;
    }
	
	/**
	 * 自定义响应
	 * @param status
	 * @param message
	 * @return
	 */
	public static ResultResponse getCustomerInstance(String status,String message) {
	    ResultResponse resultResponse = new ResultResponse();
        resultResponse.setStatusCode(status);
        resultResponse.setStatusMessage(StringUtils.isNotBlank(message) ? message : ResponseEnum.R200.getMessage());
        return resultResponse;
    }
	
	/**
	 * 自定义响应失败结果
	 * @param e
	 * @return
	 */
	public static ResultResponse getFailInstance(Exception e) {
        ResultResponse resultResponse = new ResultResponse();
        resultResponse.setStatusCode(ResponseEnum.R503.getCode());
        if (e instanceof CustomerException) {
            CustomerException customerException = (CustomerException) e;
            resultResponse.setStatusMessage("[" + customerException.getCode() + "]" + customerException.getMessage());
        } else {
            resultResponse.setStatusMessage(ResponseEnum.R503.getMessage());
        }
        return resultResponse;
    }

	public String getStatusCode() {
		return statusCode;
	}

	public void setStatusCode(String statusCode) {
		this.statusCode = statusCode;
	}

	public String getStatusMessage() {
		return statusMessage;
	}

	public void setStatusMessage(String statusMessage) {
		this.statusMessage = statusMessage;
	}

	public Object getData() {
		return data;
	}

	public void setData(Object data) {
		this.data = data;
	}

}
